package com.example.protestgps;


import java.util.ArrayList;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.LatLng;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;


public class GetLocation implements LocationListener{
	
	private LocationManager m_lm;
	private Location m_loc;
	private Context m_con;

	// Global variable
	public double m_lat = 0;
	public double m_lot = 0;
	public double mlat = 0;
	public double mlng = 0;
//	public GoogleMap map;
	public GoogleMap Mapp;
	
	ArrayList<LatLng>  mMarkerPointss;
	public LatLng pointt; 
	
	 public GetLocation(){}
		
	 public GetLocation(LocationManager lm,Context con,GoogleMap Map) {
			this.m_lm = lm ;
			Log.d("this.m_lm",""+this.m_lm );
			Log.d("lm",""+lm );
			this.m_con = con;
			this.Mapp =  Map;
			Log.d("this.myMap",""+this.Mapp);
			
			Criteria cr = new Criteria();
			String pr = lm.getBestProvider(cr, true);
			m_loc = lm.getLastKnownLocation(pr);

			 		
			lm.requestLocationUpdates(pr, 20000, 0, this);
			getLatLot();
		}

		public void getLatLot() {
			if (m_loc != null) {
				onLocationChanged(m_loc);
			}
			m_lat = (double) m_loc.getLatitude();
			m_lot = (double) m_loc.getLongitude();
			
		}

	@Override
	public void onLocationChanged(Location arg0) {
		// TODO Auto-generated method stub
		
		MainActivity main = new MainActivity();
 		mMarkerPointss = new ArrayList<LatLng>(); 
 		mMarkerPointss = main.mMarkerPoints;
 		//map = main.myMap;
		Log.d("mMarkerPoints"," "+mMarkerPointss);
		Log.d("mMarkerPoints1"," "+main.mMarkerPoints);
		
		if(mMarkerPointss.size()<1)
		{
			
		  Log.d("mmm","err1");
		
		  mlat = m_loc.getLatitude();
		  mlng = m_loc.getLongitude();
		  //Toast.makeText(this.m_con, "lat:"+mlat+"lng: "+mlng, Toast.LENGTH_SHORT).show();
		  GetLat(); 
		  GetLng();
		  pointt = new LatLng(mlat, mlng);
		  Log.d("pointt"," "+pointt);
		
		
		}
		
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}

	
	
	public double GetLat(){
		return mlat;
	}

	public double GetLng(){
		return mlng;
	}

}
